package com.weiyuwqj.api.util;

import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.util.MyAssertUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by Administrator on 2017/4/18.
 */
public class AutoUtils {

    /**
     * 判断项目是否还有余额，以及投资金额是否小于项目余额
     * @param amount                投资金额
     * @param itemBalance           项目余额
     * @return                      本次实际投资金额
     */
    public static BigDecimal checkItemBlanceIsEnoughToInvest(BigDecimal amount,BigDecimal itemBalance){
        MyAssertUtils.isTrue(BigDecimal.ZERO.compareTo(itemBalance)<0,"item.invest.item_no_more_balance");
        return amount.compareTo(itemBalance)!=1 ? amount : itemBalance;
    }

    /**
     * 获取用户账户本次可以投资金额,如果账户余额少于规则金额，则取账户余额，否则取规则金额
     * @param accountCash           账户余额
     * @param autoAmount            规则金额
     * @return                      本次可以投资金额
     */
    public static BigDecimal getInvestAmount(BigDecimal accountCash,BigDecimal autoAmount){
        return accountCash.compareTo(autoAmount)<0 ? accountCash : autoAmount;
    }

    /**
     * 包计划计算收益利率
     * @param cycleUnit
     * @param cycle
     * @param rate
     * @return
     */
    public static BigDecimal getEarnRate(int cycleUnit,int cycle,BigDecimal rate){
        switch (cycleUnit){
            case 1 :
                return  rate.multiply(new BigDecimal(cycle)).
                        divide(new BigDecimal(360), 6, BigDecimal.ROUND_DOWN);
            case 2 :
                return  rate.multiply(new BigDecimal(cycle)).
                        divide(new BigDecimal(52), 6, BigDecimal.ROUND_DOWN);
            case 3 :
                return  rate.multiply(new BigDecimal(cycle)).
                        divide(new BigDecimal(12), 6, BigDecimal.ROUND_DOWN);
            case 4 :
                return  rate.multiply(new BigDecimal(cycle)).
                        divide(new BigDecimal(4),6,BigDecimal.ROUND_DOWN);
            case 5 :
                return  rate.multiply(new BigDecimal(cycle)).
                        divide(new BigDecimal(1), 6, BigDecimal.ROUND_DOWN);
            default: return BigDecimal.ZERO;
        }
    }
}
